﻿define([
  'models/abmManagers/groupManager',
  'text!templates/abmManagers/abmBaseTmp.html',
  'text!templates/abmManagers/abmTableTmp.html',
  'text!templates/abmManagers/group/common/groupManager.html',
], function (Manager, BaseTmp, TableTmp, UniqTmp) {
    var view = Backbone.View.extend({
        el: $("#main"),
        _uId: "",
        render: function () {
            $("#mainLoading").show();
            $("div[data-rel='abm-group']").remove();
            var model = new Manager();
            var _this = this;
            this._uId = $.uuid();

            model.List(__currentSession.companyId, function (dataGroup) {
                var baseTemplate = Handlebars.compile(BaseTmp);
                var tableTemplate = Handlebars.compile(TableTmp);
                var uniqTemplate = Handlebars.compile(UniqTmp);

                var dataBaseTemplate = {
                    id: _this._uId,
                    dataRel: "abm-group", // cuidado, ref en js
                    tmpClass: "group-manager", // cuidado, ref en css
                    title: translate("_groupManager")
                };
                var dataTableTemplate = {
                    headers: [translate("_code"), translate("_name"), translate("_options")]
                };
                var dataUniqTemplate = {
                    data: dataGroup
                };

                var html = baseTemplate(dataBaseTemplate);
                var html2 = tableTemplate(dataTableTemplate);
                var html3 = uniqTemplate(dataUniqTemplate);

                _this.$el.append(html);
                $("div[data-id='" + _this._uId + "']").find("div.abm-content").append(html2);
                $("div[data-id='" + _this._uId + "']").find("tbody").append(html3);

                _this.addTableHandlers($("div[data-id='" + _this._uId + "']").find("table"));
                $("div[data-id='" + _this._uId + "']").find("table").cubeTable();
                $('<a href="#" class="btn btn-addgroup"><i class="icon-plus"></i></a>').appendTo('div.data-table-options');
                
                _this.addHandlers();
                $("#mainLoading").hide();
            });
        },
        edit: function (e) {
            var groupId = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/abmManagers/groupManager'], function (Controller) {
                (new Controller()).showEdit(groupId);
            });

        },
        deleteGroup: function (e) {
            var id = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/common/showModal'], function (Controller) {
                (new Controller()).show(
                    {
                        title: translate("_deleteGroup"),
                        text: translate("_sure_delete_group"),
                        buttons: [
                            { "title": translate("yes"), "css": "primary", "code": "yes" },
                            { "title": translate("no"), "code": "no" }
                        ],
                        callback: function (code) {
                            if (code == "yes") {
                                var model = new Manager();
                                $("#mainLoading").show();
                                model.DeleteGroup(id, function () {
                                    $("#mainLoading").hide();
                                    require(['controllers/abmManagers/groupManager'], function (Controller) {
                                        (new Controller()).show();
                                    });
                                });
                            }
                        }
                    });
            });
        },
        addGroup: function () {
            require(['controllers/abmManagers/groupManager'], function (Controller) {
                (new Controller()).addGroup();
            });
        },
        close: function () {
            var key = $("#currentTask").attr("data-rel");
            require(['controllers/home/home'], function (Controller) {
                (new Controller()).removeTask(key);
            });
        },
        addTableHandlers: function (oTable) {
            $(oTable).find("button.btn-edit").on("click", this.edit);
            $(oTable).find("button.btn-delete").on("click", this.deleteGroup);
        },
        addHandlers: function () {
            $("div[data-id='" + this._uId + "']").find("div.data-table-options a.btn-addgroup").on("click", this.addGroup);
            $("div[data-id='" + this._uId + "']").find("div.abm-top-options a.btn-close").on("click", this.close);
        }
    });
    return view;
});